package top.hidoctor.auscultation.net.p;

import android.content.Context;

import com.tang.base.BaseResponse;
import com.tang.base.NetMapEncryptUtils;
import com.tang.base.PatchRequest;
import com.tang.net.SuccessConsumer;

import java.io.File;
import java.util.Map;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import top.hidoctor.auscultation.bean.PatientBean;
import top.hidoctor.auscultation.net.PresenterWrapper;
import top.hidoctor.auscultation.net.c.PatientDetailContract;
import top.hidoctor.auscultation.net.req.PatientReq;

public class PatientDetailPresenter extends PresenterWrapper<PatientDetailContract.View> implements PatientDetailContract.Presenter {


    public PatientDetailPresenter(Context context, PatientDetailContract.View view) {
        super(context, view);
    }

    @Override
    public void getDetail(String id) {
        mView.showLoading();
        Map<String, Object> map = new NetMapEncryptUtils("").getMaps();
        add(mService.patientDetail(id, map)
                .compose(this.<PatientBean>getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<PatientBean>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<PatientBean> response) {
                        mView.hideLoading();
                        mView.getDetailScu(response.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void updatePetient(String id, PatientReq req) {
        mView.showLoading();
        Map<String, Object> map = new NetMapEncryptUtils(req).getMaps();
        PatchRequest patchRequest = new PatchRequest(map);
        add(mService.patientDetailUpdate(id, patchRequest.getRequestBody())
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        mView.getUpadatDetailScu();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void addPatient(PatientReq req) {
        mView.showLoading();
        Map<String, Object> map = new NetMapEncryptUtils(req).getMaps();
        PatchRequest patchRequest = new PatchRequest(map);
        add(mService.addPatient(patchRequest.getRequestBody())
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        mView.addPatientSuc();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    public void uploadFile(String uri) {
        File file = new File(uri);
        RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpeg;charset=utf-8"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("fileList", file.getName(), requestFile);
        RequestBody type = RequestBody.create(MediaType.parse("multipart/form-data"), "0");
        mView.showLoading();
        add(mService.uploadFile(type, body)
                .compose(this.<String>getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<String>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<String> response) {
                        mView.hideLoading();
                        mView.getFileScu(response.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }
}
